package GradeCalculation;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author maojiangyue
 * @Date 2025/10/18
 * @Description 唱歌比赛得分-去除最高最低后取平均得总分
 */
public class SingingCompetition implements GradeCount{
    @Override
    public double averageScore(List<Double> score) {
        if(score==null || score.isEmpty()){
            return 0;
        } else if (score.size() == 1){
            return score.getFirst();
        } else if (score.size() == 2) {
            return (score.get(0)+score.get(1))/2;
        }

        //通过创建可变长度集合，避免对定长集合进行赋值操作
        List<Double> scoreCopy = new ArrayList<>(score);

        //冒泡排序
        for (int i = 0; i < scoreCopy.size() - 1; i++) {
            for (int j = 0; j < scoreCopy.size() - 1 - i; j++) {
                if (scoreCopy.get(j) > scoreCopy.get(j + 1)) {
                    double temp = scoreCopy.get(j);
                    scoreCopy.set(j, scoreCopy.get(j + 1));
                    scoreCopy.set(j + 1, temp);
                }
            }
        }
        //可替换为List类提供的排序方法
        //scoreCopy.sort(Double::compareTo);
        double total=0;
        for (int i = 1; i < scoreCopy.size()-1; i++) {
            total+=scoreCopy.get(i);
        }
        return total/(scoreCopy.size()-2);
    }

    @Override
    public String getName() {
        return "唱歌比赛";
    }
}
